package com.ssm.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ssm.dao.IRoleDao;
import com.ssm.entity.Role;

@Service
public class RoleServiceImpl implements IRoleService{
	@Autowired
	private IRoleDao roleDao;
	@Override
	public String findAll() {
		List<Role> list = roleDao.findAll();
		JSONObject obj = new JSONObject();
		obj.put("draw", 1);
		obj.put("recordsTotal", roleDao.count());
		obj.put("recordsFiltered", roleDao.count());
		JSONArray data = new JSONArray();
		for(Role item:list) {
			JSONArray arr = new JSONArray();
			arr.add(item.getRid());
			arr.add(item.getRname());
			data.add(arr);
		}
		obj.put("data", data);
		return obj.toJSONString();
	}

	@Override
	public String add(Role role) {
		int count = roleDao.add(role);
		String status = "ok";
		String msg = "";
		if(count<1) {
			status = "error";	
			msg = "添加失败";
		}
		JSONObject obj=new JSONObject();
		obj.put("Status", status);
		obj.put("msg", msg);
		return obj.toJSONString();
	}

	@Override
	public String get(int id) {
		Role role = roleDao.get(id);
		JSONObject obj=new JSONObject();		 
		obj.put("rid", role.getRid());
		obj.put("rname", role.getRname());
		return obj.toJSONString();
	}

	@Override
	public String update(Role role) {
		int count = roleDao.update(role);
		String status = "ok";
		String msg = "";
		if(count<1) {
			status = "error";	
			msg = "更新失败";
		}
		JSONObject obj=new JSONObject();
		obj.put("Status", status);
		obj.put("msg", msg);
		return obj.toJSONString();
	}

	@Override
	public String delete(int id) {
		int count = roleDao.delete(id);
		String status = "ok";
		String msg = "";
		if(count<1) {
			status = "error";	
			msg = "删除失败";
		}
		JSONObject obj=new JSONObject();
		obj.put("Status", status);
		obj.put("msg", msg);
		return obj.toJSONString();
	}

}
